package com.jye.cloud.user.controller;



import com.jye.cloud.Constants.MyConstants;
import com.jye.cloud.domain.Comments;
import com.jye.cloud.domain.CommentsReply;
import com.jye.cloud.user.service.CommentsReplyService;
import com.jye.cloud.user.service.CommentsService;
import com.jye.cloud.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
@Api(tags="评论控制器API")
public class CommentsController {

    @Autowired
    CommentsService commentsService;

    @Autowired
    CommentsReplyService commentsReplyService;

    @GetMapping("/findCoursesCommentsByPId")
    @ApiOperation(value="查找课程评论", notes="根据父ID查找相关评论", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "pId",
                    value = "复用父ID", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findCoursesCommentsByPId(int pId, int skip, int limit){
        JsonResult jsonResult = new JsonResult();
        //复用类型
        int type = MyConstants.commentsType.coursesType;
        List<Comments> commentsByTypeAndPId = commentsService.findCommentsByTypeAndPId(type, pId, skip, limit);
        if(commentsByTypeAndPId.size()!=0){
            jsonResult.setData(commentsByTypeAndPId);
        }else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查找失败!");
        }

        return jsonResult;
    }




    @PostMapping("/commentCourseByComment")
    @ApiOperation(value="评论课程", notes="用户Id:uId,课程id:pId,内容content", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "comments",
                    value = "评论类", required = true)

    })
    public JsonResult commentCourseByComment(Comments comments){
        JsonResult jsonResult = new JsonResult();
        int coursesType = MyConstants.commentsType.coursesType;
        comments.setType(coursesType);
        jsonResult.setMsg(commentsService.commentCourseByComment(comments));
        return jsonResult;
    }




    @PostMapping("/commentComByComment")
    @ApiOperation(value="评论某个评论", notes="用户Id:uId,评论id:pId,内容content", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "commentsReply",
                    value = "评论类", required = true)
    })
    public JsonResult commentComByComment(CommentsReply commentsReply){
        JsonResult jsonResult = new JsonResult();
        int commentType = MyConstants.commentsReplyType.commentType;
        commentsReply.setType(commentType);
        jsonResult.setMsg(commentsReplyService.commentComByComment(commentsReply));
        return jsonResult;
    }




    @PutMapping("/updateCommentByIdAndStatus")
    @ApiOperation(value="修改评论状态", notes="根据修改评论状态", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "评论id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "status",
                    value = "修改状态", required = true,
                    dataType = "Integer"),
    })
    public JsonResult updateCommentByIdAndStatus(int id, int status){
        JsonResult jsonResult = new JsonResult();
        int i = commentsService.updateCommentByIdAndStatus(id,status);
        if (i == 1){
            jsonResult.setMsg("更新成功!");
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("更新失败!");
        }
        return jsonResult;
    }


    @DeleteMapping("/deleteCommentById")
    @ApiOperation(value="删除评论", notes="根据id删除评论", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "评论id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult deleteCommentById(int id){
        JsonResult jsonResult = new JsonResult();
        int i = commentsService.deleteCommentById(id);
        if(i!=0){
            jsonResult.setMsg("删除成功!");
        }else{
            jsonResult.setMsg("删除失败!");
            jsonResult.setCode("500!");

        }
        return jsonResult;
    }


}
